//This is my controller class

#include <iostream>
#include <string>
#include "VendingMachine.h"
#include "Coin.h"
#include "Money.h"

using namespace std;
using namespace Homework;
namespace Homework
{
VendingMachine::VendingMachine(void)
{
	p = new Product();
	m = new Menu();
	c = new Coin();

	/*
	x	1.5
	abs(x)	1
	floor(x)	1
	x%floor(x)==0
	#include <math.h>
	*/

	payment = 0.0;
	payment2 = 0.0;
}

VendingMachine::~VendingMachine()
{
	delete p;
	delete m;
	delete c;
}

void VendingMachine::DisplayMenu()
{
	m->Display();
}

void VendingMachine::run()
{
	cout << "Welcome to Vending Machine" << endl;

	do
	{
		DisplayMenu();
		cin >> choice;

		switch(choice)
		{
			case 1:
				p->getProductInfo(choice-1);
				break;
			case 2:
				p->getProductInfo(choice-1);
				break;
			case 3:
				p->getProductInfo(choice-1);
				break;
			case 4:
				p->getProductInfo(choice-1);
				break;
			case 5:
				p->getProductInfo(choice-1);
				break;
			case 6:
				p->getProductInfo(choice-1);
				break;
			default:
				exit(0);
				break;
		}
	

		do
		{
			cout << "Press 1 to insert coins" << endl;
			cout << "Press 2 to insert cash" << endl;
			cout << "Press 3 to use a credit card" << endl;
			cin >> payType;


			switch(payType)
			{
				case 1:
					{
						Money m(Money::COIN);
						amountLeft = CheckPayment(m);
						break;
					}
				case 2:
					{
						Money csh(Money::CASH);
						amountLeft = CheckPayment(csh);
						break;
					}
				case 3:
					break;
				default:
					break;
			}

			if(amountLeft!=0)
					{
						cout << "Press 1 to insert more money" << endl;
						cout << "Press 2 to return money and exit" << endl;
						cin >> choice2;
					}
					if(choice2 == 2)
					{
						cout << "Dispensing " << c->getPayment(choice) << endl;
						cout << "Please take change. Thank you" << endl;
						exit(0);
					}
			}while(amountLeft !=0);
	}while(choice != 7);

}

double VendingMachine::CheckPayment(Money & m)
{
	checkAmount = Check(m);

	if(checkAmount == true)
	{
		cout << "Dispensing..." << endl;
		cout << "Please take item" << endl;
		amountLeft = 0;
		return amountLeft;
	}
	else 
	{
		amountLeft = getPayment(choice);
		cout << "You still owe " << amountLeft << endl;
		return amountLeft;

	}
}

bool VendingMachine::Check(Money & m)
{
	do
	{	//cin.ignore();
		//cin.clear();
		
		cout << "Please insert amount" << endl;
		cin >> payment2;

		m.value = payment2;

		if(m.value <= 0)
		{
			cout << "Not an actual amount of money. Please try again" << endl;
			//cin.ignore();
			//cin.clear();
		}
	}while(m.value <= 0);


	payment = payment + m.value;

	if(p->food[(Homework::PType)(choice-1)].price == payment)
	{
		return true;
	}
	else if(payment > p->food[(Homework::PType)(choice-1)].price)
	{
		cout << "Your change is " << (payment-p->food[(Homework::PType)(choice-1)].price) << endl;
		return true;
	}
	else
	{
		return false;
	}
}

double VendingMachine::getPayment(int selection)
{
	return p->food[(Homework::PType)(selection-1)].price - payment;
}
}